package com.stylefeng.guns;

import com.stylefeng.guns.modular.nettywebsocket.ChatServer;
import com.stylefeng.guns.modular.rocketmq.RocketMQConsumer;
import io.netty.channel.ChannelFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.net.InetSocketAddress;

/**
 * SpringBoot方式启动类
 *
 * @author stylefeng
 * @Date 2017/5/21 12:06
 */
@SpringBootApplication
public class GunsApplication implements CommandLineRunner{

    @Autowired
    private ChatServer chatServer;


    private final static Logger logger = LoggerFactory.getLogger(GunsApplication.class);

    @Bean
    public ChatServer chatServer() {
        return new ChatServer();
    }


    public static void main(String[] args) {
        SpringApplication.run(GunsApplication.class, args);

        logger.info("GunsApplication is success!");
    }

    @Override
    public void run(String... strings) throws Exception {
        InetSocketAddress address = new InetSocketAddress("localhost", 9090);
        ChannelFuture future = chatServer.start(address);

        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                chatServer.destroy();
            }
        });

        future.channel().closeFuture().syncUninterruptibly();
    }
}
